home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 426-450 / disk_441 / dme / src / rexx.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  5KB  |  255 lines

  1.  
  2. /*
  3.  *  REXX.C
  4.  *
  5.  *    (c) Copyright 1987 by Kim DeVaughn, All Rights Reserved
  6.  *
  7.  *  ARexx interface code, etc.
  8.  *
  9.  */
  10.  
  11. #include "defs.h"
  12.  
  13. Prototype void openrexx (void);
  14. Prototype void closerexx (void);
  15. Prototype void do_rx (void);
  16. Prototype void do_rx1 (void);
  17. Prototype void do_rx2 (void);
  18. Prototype void do_rxImplied (char *, char *);
  19. Prototype int do_rexx (char *);
  20.  
  21.  
  22. #if AREXX
  23.  
  24. #include "rexx/storage.h"
  25. #include "rexx/rxslib.h"
  26. #include "rexx/rexxio.h"
  27. #include "rexx/errors.h"
  28. #include "rexx.h"
  29.  
  30.  
  31. int foundcmd;        /* control for implicit ARexx macro invocation   */
  32. int cmderr;        /* global command error flag for do_rexx()'s use */
  33.  
  34. __stkargs APTR CreateRexxMsg ARGS((PORT *, char *, char *));
  35. __stkargs void DeleteRexxMsg ARGS((struct RexxMsg *));
  36. __stkargs APTR CreateArgstring ARGS((char *, int));
  37. __stkargs void DeleteArgstring ARGS((struct RexxArg *));
  38. __stkargs int IsRexxMsg ARGS((struct RexxMsg *));
  39. __stkargs void InitPort ARGS((PORT *, char *));
  40. __stkargs void FreePort ARGS((PORT *));
  41.  
  42. struct RxsLib *RexxSysBase;
  43.  
  44. /*
  45.  * initialization for ARexx ... just open rexsyslib.library
  46.  */
  47.  
  48. void
  49. openrexx()
  50. {
  51.     RexxSysBase = (struct RxsLib *)OpenLibrary("rexxsyslib.library", (ULONG)RXSVERS);
  52.     return;
  53. }
  54.  
  55.  
  56.  
  57. /*
  58.  * cleanup any open ARexx stuff ...  just close rexsyslib.library for now
  59.  */
  60.  
  61. void
  62. closerexx()
  63. {
  64.     if (RexxSysBase)
  65.     CloseLibrary((struct Library *)RexxSysBase);
  66. }
  67.  
  68.  
  69.  
  70. /*
  71.  *  explicit invocation interface between do_command() and do_rexx
  72.  *  for ARexx macros having NO arguments (i.e., for the "rx" command)
  73.  */
  74.  
  75. void
  76. do_rx()
  77. {
  78.     do_rexx(av[1]);
  79. }
  80.  
  81.  
  82.  
  83. /*
  84.  *  explicit invocation interface between do_command() and do_rexx
  85.  *  for ARexx macros having ONE argument (i.e., for the "rx1" command)
  86.  */
  87.  
  88. void
  89. do_rx1()
  90. {
  91.     char macbuf[256];
  92.  
  93.     strcpy(macbuf, av[1]);
  94.     strcat(macbuf, " ");
  95.     strcat(macbuf, av[2]);
  96.     do_rexx(macbuf);
  97. }
  98.  
  99.  
  100.  
  101. /*
  102.  *  explicit invocation interface between do_command() and do_rexx
  103.  *  for ARexx macros having TWO arguments (i.e., for the "rx2" command)
  104.  */
  105.  
  106. void
  107. do_rx2()
  108. {
  109.     char macbuf[256];
  110.  
  111.     strcpy(macbuf, av[1]);
  112.     strcat(macbuf, " ");
  113.     strcat(macbuf, av[2]);
  114.     strcat(macbuf, " ");
  115.     strcat(macbuf, av[3]);
  116.     do_rexx(macbuf);
  117. }
  118.  
  119.  
  120.  
  121. /*
  122.  *  implicit invocation interface between do_command() and do_rexx
  123.  *  for ARexx macros implicitly called; arbitrary number of arguments
  124.  */
  125.  
  126. void
  127. do_rxImplied(cmd, args)
  128. char *cmd;
  129. char *args;
  130. {
  131.     char macbuf[256];
  132.  
  133.     strcpy(macbuf, cmd);
  134.     strcat(macbuf, " ");
  135.     strcat(macbuf, args);
  136.     do_rexx(macbuf);
  137. }
  138.  
  139. /*
  140.  *  issue a command to ARexx ...
  141.  */
  142.  
  143. int
  144. do_rexx(macstr)
  145. char *macstr;
  146. {
  147.     struct RexxArg *macarg;
  148.  
  149.     struct MsgPort  RexxPort;
  150.     struct MsgPort *ARexxPort;
  151.  
  152.     struct RexxMsg *macptr;
  153.     struct RexxMsg *cmdptr;
  154.  
  155.     char host[16];
  156.     char hexbuf[12];        /* should only need 9 bytes */
  157.     char errmsg[80];        /* don't build a larger error message */
  158.  
  159.     int  ret;
  160.     int  err;
  161.  
  162.  
  163.     if (RexxSysBase == 0) {
  164.     title("Unknown Command   -   No Macros:  ARexx Not Installed ");   /* no rexxsyslib */
  165.     return(0);
  166.     }
  167.  
  168.     clrmem(&RexxPort, sizeof(struct MsgPort));
  169.     strcpy(host, "DME");
  170.     sprintf(hexbuf, "%08x", &RexxPort);
  171.     strcat(host, hexbuf);
  172.     InitPort(&RexxPort, host);      /* need to error check this */
  173.     AddPort(&RexxPort);
  174.     /* return here if InitPort failed */
  175.  
  176.  
  177.     if (macarg = (struct RexxArg *)CreateArgstring(macstr, strlen(macstr))) {
  178.     if (macptr = (struct RexxMsg *)CreateRexxMsg(&RexxPort, "dme", host)) {
  179.         ACTION(macptr) = RXCOMM;
  180.         ARG0(macptr)   = (STRPTR)macarg;
  181.  
  182.         Forbid();
  183.         if (ARexxPort = (struct MsgPort *)FindPort("REXX")) {
  184.         PutMsg(ARexxPort, (MSG *)macptr);
  185.         Permit();
  186.         title("Calling ARexx Macro ... ");
  187.  
  188.         for (;;) {
  189.             WaitPort(&RexxPort);
  190.             cmdptr = (struct RexxMsg *)GetMsg(&RexxPort);
  191.  
  192.             if (IsRexxMsg(cmdptr)) {
  193.  
  194.             foundcmd = 0;
  195.             cmderr = CMD_INITIAL;
  196.             ret = do_command(ARG0(cmdptr));
  197.             err = cmderr;
  198.             if (foundcmd) {
  199.                 ret = (ret == 1) ? 0 : 5;   /* cmd error:  RC = 5  */
  200.             } else {
  201.                 ret = do_rexx(ARG0(cmdptr));    /* another macro? */
  202.             }
  203.  
  204.             RESULT1(cmdptr) = ret;
  205.             RESULT2(cmdptr) = 0;
  206.             ReplyMsg((MSG *)cmdptr);
  207.             }
  208.             do_command("null");     /* a kludge to set "foundcmd" */
  209.             if (macptr == cmdptr) break;
  210.         }
  211.  
  212.  
  213.         if (ret = RESULT1(cmdptr)) {
  214.             if (RESULT2(cmdptr)) {
  215.             if (RESULT2(cmdptr) == 1) {
  216.                 title("Unknown Command ");
  217.             } else {
  218.                 sprintf(errmsg, "ARexx Macro Error:  Code = %d  Severity = %d ", RESULT2(cmdptr), ret);
  219.                 title(errmsg);
  220.             }
  221.             } else {
  222.             sprintf(errmsg, "User Specified Macro Error:  RC = %d ", ret);
  223.             title(errmsg);
  224.             }
  225.         } else {
  226.             if (err <= TITLE_THRESHHOLD) {
  227.             title("OK ");
  228.             }
  229.         }
  230.         ret = ret + err;
  231.         } else {
  232.         Permit();
  233.         title("Unknown Command   -   No Macros:  ARexx Not Active ");   /* no REXX port */
  234.  
  235.         ret = -1;
  236.         }
  237.         DeleteRexxMsg(macptr);
  238.     } else {
  239.         title("CreateRexxMsg() Failed ");   /* may be overkill, and not need to ckeck this */
  240.         ret = -1;
  241.     }
  242.     DeleteArgstring(macarg);
  243.     } else {
  244.     title("CreateArgstring() Failed ");     /* may be overkill, and not need to check this */
  245.     ret = -1;
  246.     }
  247.  
  248.     RemPort(&RexxPort);
  249.     FreePort(&RexxPort);
  250.     return(ret);
  251. }
  252.  
  253. #endif
  254.  
  255.